package cn.itrip.biz.service.itripComment;
import cn.itrip.mapper.ItripComment.ItripCommentMapper;
import cn.itrip.mapper.ItripHotelOrder.ItripHotelOrderMapper;
import cn.itrip.mapper.ItripImage.ItripImageMapper;
import cn.itrip.pojo.ItripComment;
import cn.itrip.common.*;
import cn.itrip.pojo.ItripHotelOrder;
import cn.itrip.pojo.ItripImage;
import cn.itrip.vo.comment.ItripHotelContent;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.lang.ref.PhantomReference;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import cn.itrip.common.Constants;
@Service
public class ItripCommentServiceImpl implements ItripCommentService {

    @Resource
    private ItripCommentMapper itripCommentMapper;
    @Resource
    private ItripImageMapper itripImageMapper;
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;

    public ItripComment getItripCommentById(Long id)throws Exception{
        return itripCommentMapper.getItripCommentById(id);
    }

    @Override
    public ItripHotelContent getCount(String hotelId) throws Exception {
        return itripCommentMapper.getCount(hotelId);
    }

    @Override
    public List<ItripComment> getItripCommentByHotelId(String hotelId) throws Exception {
        return itripCommentMapper.getItripCommentByHotelId(hotelId);
    }

    public List<ItripComment>	getItripCommentListByMap(Map<String,Object> param)throws Exception{
        return itripCommentMapper.getItripCommentListByMap(param);
    }

    public Integer getItripCommentCountByMap(Map<String,Object> param)throws Exception{
        return itripCommentMapper.getItripCommentCountByMap(param);
    }

    public Integer itriptxAddItripComment(ItripComment itripComment)throws Exception{
            itripComment.setCreationDate(new Date());
            return itripCommentMapper.insertItripComment(itripComment);
    }

    @Override
    public void itriptxAddItrip(ItripComment itripComment, List<ItripImage> imageList) throws Exception {
        // 计算综合得分 = (设施 + 卫生 + 服务 + 位置) / 4
        int sum = itripComment.getFacilitiesScore() + itripComment.getHygieneScore()
                + itripComment.getPositionScore() + itripComment.getServiceScore();
        itripComment.setScore(sum / 4);
        // 保存评论
        itripCommentMapper.insertItripComment(itripComment);

        System.out.println(itripComment.getOrderId());

        //获取ItripComment
        List<ItripComment> itripCommentByOrderId = itripCommentMapper.getItripCommentByOrderId(itripComment.getOrderId());

        // 保存图片
        if (EmptyUtils.isNotEmpty(imageList)) {
            for (ItripImage image : imageList) {

                image.setTargetId(itripCommentByOrderId.get(1).getId());

                itripImageMapper.insertItripImage(image);
            }
        }
        // 变更订单状态 -》已评论
        ItripHotelOrder order = new ItripHotelOrder();
        order.setId(itripComment.getOrderId());
        order.setOrderStatus(4);
        order.setModifyDate(itripComment.getCreationDate());
        order.setModifiedBy(itripComment.getCreatedBy());
        itripHotelOrderMapper.updateItripHotelOrder(order);
    }

    public Integer itriptxModifyItripComment(ItripComment itripComment)throws Exception{
        itripComment.setModifyDate(new Date());
        return itripCommentMapper.updateItripComment(itripComment);
    }

    public Integer itriptxDeleteItripCommentById(Long id)throws Exception{
        return itripCommentMapper.deleteItripCommentById(id);
    }

    public Page<ItripComment> queryItripCommentPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize)throws Exception{
        Integer total = itripCommentMapper.getItripCommentCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripComment> itripCommentList = itripCommentMapper.getItripCommentListByMap(param);
        page.setRows(itripCommentList);
        return page;
    }

}
